package org.sandbox2.web;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.sandbox2.domain.bank.Account;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RooWebScaffold(path = "accounts", formBackingObject = Account.class)
@RequestMapping("/accounts")
@Controller
public class AccountController {
	
	@RequestMapping(method = RequestMethod.POST)
    public String create(@Valid Account account, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            uiModel.addAttribute("account", account);
            return "accounts/create";
        }
        uiModel.asMap().clear();
        account.create();
        return "redirect:/accounts/" + encodeUrlPathSegment(account.getId().toString(), httpServletRequest);
    }
}
